var app = getApp();
var api = require('../../../utils/api.js');
var $= require('../../../utils/util.js');
import { platform } from '../../../disposition.js'
var Store = app.Store

Page({
  data: {
   
  },
  onLoad: function (e) {
   
  },
  //首次用户授权
  bindGetUserInfo: function (e) {//用户点了允许授权按钮 
    if (e.detail.userInfo) { // 点击确定了
      wx.login({
        success: (res) => {
          var params = {
            code: res.code,
            rawData: e.detail.userInfo,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            platform
          };
          api.$post(api.main.login, params).then((res) => {
            if (res.code == 0){
              wx.setStorageSync('XAccessToken', res.data.access_token)
              wx.setStorageSync('user_id', res.data.user_id)
              wx.setStorageSync('avatar_url', res.data.avatarUrl)
              Store.dispatch({ type: 'SET_IS_FROM_AUTHOR', isFromAuthor: true }) 
              setTimeout(()=>{
                wx.navigateBack({
                  delta: 1
                })
              },1000)
            } else {
              $.toast('授权失败')
            }
          })
        },
        fail:  (res) => {
          console.log(res, '&&登录失败&&')
        }
      })
    } else { // 点击取消了
      $.toast('需要授权哦')
    }
  }
})